//
//  FDWeatherModel.m
//  FamousDoctorOnline
//
//  Created by 应谨鸿 on 16/7/6.
//  Copyright © 2016年 wszyxc. All rights reserved.
//

#import "FDWeatherModel.h"
#import "AFNetworking.h"
#import "MJExtension.h"
#import "FDWeatherView.h"
#import "NetWorkTool.h"

@implementation FDWeatherModel

+ (instancetype)shareManager{
    
    static FDWeatherModel *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        instance = [[FDWeatherModel alloc] init];
        
    });
    return instance;
}

//提供模型数组,异步网络,解析到模型--block回调
- (void)loadWeatherWithCity:(NSString *)city success:(void(^)(NSArray<FDWeatherModel *> *headlineList))successBlock error:(void(^)())errorBlock{
    
    NSString *url = @"http://api.map.baidu.com/telematics/v3/weather";

    NSDictionary *paramDic = @{@"location":city,
                               @"output":@"json",
                               @"ak":@"17IvzuqmKrK1cGwNL6VQebF9"
                               };
    
//get
    [[NetWorkTool shareManager] GET_RequestWithUrlString:url parmas:paramDic successBloock:^(id responseObject) {

        NSArray *respon = responseObject[@"results"];
        
        NSArray *array = [FDWeatherModel mj_objectArrayWithKeyValuesArray:respon[0][@"weather_data"]];
        
        NSArray <FDWeatherModel *>*temp = array;
        //遍历数组
        for (int i = 0; i < array.count; i++) {
            
            temp[i].currentCity = respon[0][@"currentCity"];
        }
        array = temp;
        
        if (successBlock) {
            successBlock(array);
        }
    } failureBlock:^(NSError *error) {
        if (errorBlock) {
            
            errorBlock();
        }
    }];

}
@end
